package com.xing.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xing.gulimall.product.entity.AttrEntity;
import com.xing.gulimall.product.service.AttrAttrgroupRelationService;
import com.xing.gulimall.product.service.AttrService;
import com.xing.gulimall.product.vo.AttrGroupRelationVo;
import com.xing.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xing.gulimall.product.entity.AttrGroupEntity;
import com.xing.gulimall.product.service.AttrGroupService;
import com.xing.common.utils.PageUtils;
import com.xing.common.utils.R;


/**
 * 属性分组 json生成的七个实体类
 *
 * @author xingfuhao
 * @email 2030280200@qq.com
 * @date 2022-08-31 10:41:37
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 获取分类下所有分组与关联属性
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        //1.查询当前分类下的所有属性分组。
        //2.查出每一个属性分组的所有属性。
      List<AttrGroupWithAttrsVo>  vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
      return R.ok().put("data",vos);
    }




    /**
     * 添加属性与分组关联方法
     *
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatchs(vos);
        return R.ok();
    }


    /**
     * 获取属性分组没有关联的其他属性
     *
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }


    /**
     * 属性分组的关联列表 移除
     *
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }


    /**
     * 属性分组的关联已存在列表
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entities);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R listPage(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 分页查询分类属性分组
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 保存关联关系
     * 保存多个值,来操作
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody AttrGroupRelationVo... attrGroupRelationVos) {
        attrGroupService.saveRelation(attrGroupRelationVos);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */

    @PostMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }


    @GetMapping("/{attrGroupId}/noattr/relation")
    public R listNoAttrRelation(@RequestParam Map<String, Object> params,
                                @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrGroupService.NoAttrRelation(params, attrGroupId);
        return R.ok().put("page", page);
    }


}
